package com.opages.netty.demo.netty.rpc.common;

import java.lang.reflect.Method;

/**
 * @author daizhong.liu
 * @create 2021-12-15 17:50
 **/
public class RpcInvoker {
    private Method method;
    private Object[] params;
    private Object result;

    public RpcProtocol toProtocol() {
        String className = method.getDeclaringClass().getCanonicalName();
        String methodName = method.getName();
        StringBuilder param = new StringBuilder();
        if(params != null && params.length > 0) {
            for (int i=0;i<params.length;i++) {
                param.append(params[i].toString()).append("&");
            }
            param.deleteCharAt(param.length()-1);
        }
        RpcProtocol protocol = new RpcProtocol();
        protocol.setClassName(className.getBytes());
        protocol.setMethodName(methodName.getBytes());
        protocol.setParams(param.toString().getBytes());
        return protocol;
    }

    public Method getMethod() {
        return method;
    }

    public void setMethod(Method method) {
        this.method = method;
    }

    public Object[] getParams() {
        return params;
    }

    public void setParams(Object[] params) {
        this.params = params;
    }

    public Object getResult() {
        return result;
    }

    public void setResult(Object result) {
        this.result = result;
    }
}
